import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { TUsers } from 'src/entities/users.entity';

export class UserRepository {
  constructor(
    @InjectRepository(TUsers)
    private readonly userRepository: Repository<TUsers>,
  ) {}

  //   用户登录
  loginRepository(account: string) {
    return this.userRepository.findOne({
      where: { account },
    });
  }

  //   用户注册
  async registerRepository(params: any): Promise<TUsers> {
    return this.userRepository.save(params);
  }

  //   查询是否有重复账号
  async queryAccountRepository(account: string) {
    return this.userRepository.findOne({
      where: {
        account,
        delFlag: 0,
      },
    });
  }

  //   查询用户列表
  async queryUsersRepository() {
    return this.userRepository.find({
      where: {
        delFlag: 0,
      },
    });
  }

  //   查询用户详情信息
  queryDetailsRepository(id: number) {
    return this.userRepository.findOne({
      where: { id, delFlag: 0 },
    });
  }

  //   修改用户信息
  updateUserInfoRepository(id: number, params: any) {
    return this.userRepository.update(id, params);
  }

  //   删除用户
  deleteUserRepository(id: number) {
    return this.userRepository.update(id, { delFlag: 1 });
  }

  //   注销用户
  logoutRepository(id: number) {
    return this.userRepository.update(id, { delFlag: 1, state: '4' });
  }

  //   TODO: 修改用户状态

  //   TODO: 修改用户密码
}
